
import axios from 'axios';
import { Message } from 'element-ui';
import router from '@/router/index.js'


const http = axios.create({
    headers: {
        'content-type': 'application/json;charset=UTF-8',
    },
    withCredentials: false
});


// 添加请求拦截器
http.interceptors.request.use((config)=> {
    config.timeout = 30000;
    return config;
}, (error)=> {
    // 对请求错误做些什么
    return Promise.reject(error);
});
  
// 添加响应拦截器
http.interceptors.response.use((response)=>{
        const data = response.data || {};
        if(data.status == 401){
            Message({
                type:'error',
                message:data.msg,
                duration:1500,
            });
            setTimeout(() => {
                router.replace('/');
            }, 1500);
            return;
        }
        else if(data.status == 500){
            Message({
                type:'error',
                message:data.msg,
                duration:1500,
            });
            return;
        }
        return data;
    },
    (error)=> {
    // 对响应错误做点什么
    return Promise.reject(error);
});

//config/index.js proxyTable 配置跨域
const get = (path,data={})=>{
    const url = (process.env.NODE_ENV == 'development' ? '/apiProxy' : window.sysModel.baseUrl) + path;
   
    data.t = new Date().getTime();
    return http.get(url,{
        params:data
    });
};


const post = (path,data={})=>{
    const url = (process.env.NODE_ENV == 'development' ? '/apiProxy' : window.sysModel.baseUrl) + path;
   
    data.t = new Date().getTime();
    return http.post(url,data);
};


export default {
    get,
    post,
};